package Offer来了.设计模式.单例模式;

//枚举实现单例模式测试
public class ClassFactory {
    private enum MyEnumSingleton {
        INSTANCE;
        private MySingleton instance;

        //枚举类的构造方法在类加载时被初始化
        private MyEnumSingleton() {
            instance = new MySingleton();
        }

        public MySingleton getInstance() {
            return instance;
        }
    }

    public static MySingleton getInstance() {
        return MyEnumSingleton.INSTANCE.getInstance();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                System.out.println(ClassFactory.getInstance().hashCode());
            }).start();
        }
    }
}

class MySingleton {
    public MySingleton() {
    }
}
